package com.zb.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zb.dao.BaseDao;
import com.zb.dao.HelpDao;
import com.zb.entity.Car;
import com.zb.entity.Help;
import com.zb.entity.Store;

public class HelpDaoImpl implements HelpDao{
	private BaseDao db=new BaseDao();
	public List<Help> getHelpList(Integer index, Integer size, String carnum) {
		List<Help> list=new ArrayList<Help>();
		String sql="SELECT c.id,help.`helpnum`,help.`helpname`,help.`helpDate`,help.`HelpMoney`,sto.`StoreName`,help.`Remake` \r\n" + 
				"				FROM CAR AS c JOIN `help` AS `help`  ON c.`id`=help.`Id` JOIN store AS sto ON help.`helpShop`=sto.`id`";
		if(carnum!=null&&"".equals(carnum)) {
			sql+=" and carnum like '%" + carnum + "%'";
		}
		sql+=" order by carnum limit ?,?";
		ResultSet rs=db.query(sql, (index-1)*size,size);
		try {
			while(rs.next()) {
				Car c=new Car();
				Help h=new Help();
				Store s=new Store();
				c.setId(rs.getInt(1));
				h.setHelpNum(rs.getString(2));
				h.setHelpName(rs.getString(3));
				h.setHelpDate(rs.getString(4));
				h.setHelpMoney(rs.getString(5));
				s.setStoreName(rs.getString(6));
				h.setRemake(rs.getString(7));
				h.setCar(c);
				h.setStore(s);
				list.add(h);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.close();
		}
		return list;
	}

	public int getHelpCount(String carnum) {
		String sql="SELECT COUNT(*)FROM CAR AS c JOIN `help` AS `help`  ON c.`id`=help.`Id` JOIN store AS sto ON help.`helpShop`=sto.`id`";
		if(carnum!=null&&"".equals(carnum)) {
			sql+=" and carnum like '%" + carnum + "%'";
		}
		int count=0;
		ResultSet rs=db.query(sql);
		try {
			if(rs.next()) {
				count=rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.close();
		}
		return count;
	}

	public void addHelp(Help h) {
		// TODO Auto-generated method stub
		
	}

	public void update(Help h) {
		// TODO Auto-generated method stub
		String sql="update help set helpnum=?,helpname=?,HelpDate=NOW(),helpmoney=?,remake=? where id=?";
		db.update(sql, h.getHelpNum(),h.getHelpName(),h.getHelpMoney(),h.getRemake(),h.getCar().getId());
		db.close();
	}

	public void delHelp(int id) {
		// TODO Auto-generated method stub
		String sql="delete from help where id=?";
		db.update(sql, id);
		db.close();
	}

	public Help getHelpById(int id) {
		String sql="select*from help where id=?";
		ResultSet rs=db.query(sql, id);
		try {
			if(rs.next()) {
				Car c=new Car();
				Help h=new Help();
				Store s=new Store();
				c.setId(rs.getInt(1));
				h.setHelpNum(rs.getString(2));
				h.setHelpName(rs.getString(3));
				h.setHelpDate(rs.getString(4));
				h.setHelpMoney(rs.getString(5));
				s.setId(rs.getInt(6));
				h.setRemake(rs.getString(7));
				h.setCar(c);
				h.setStore(s);
				return h;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			db.close();
		}
		return null;
	}

}
